package com.eight.cloud.job.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eight.cloud.core.global.result.Result;
import com.eight.cloud.job.common.constants.ScheduleConstants;
import com.eight.cloud.job.model.dto.ExecutionJobDto;
import com.eight.cloud.job.model.dto.ExecutionJobQueryDto;
import com.eight.cloud.job.model.vo.JobDataMapVo;
import com.eight.cloud.job.service.IExecutionJobService;
import com.eight.cloud.job.util.CronUtils;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description: 定时任务调度信息操作处理
 * @ClassName: ScheduleController
 * @Author: TXC
 * @Date: 2025-02-24 18:35
 **/
@RequestMapping("/execution")
@RestController
public class ExecutionJobController {
    @Resource
    private IExecutionJobService executionJobService;

    @PostMapping("/add")
    public Result<String> add(@RequestBody ExecutionJobDto executionJob) throws SchedulerException {
        if (CronUtils.isValid(executionJob.getCronExpression())) {
            executionJobService.addCronJob(executionJob);
            return Result.ok();
        }
        return Result.fail("新增任务'" + executionJob.getUuid() + "'失败，Cron表达式不正确");
    }

    /**
     * 查询定时任务列表
     */
    @GetMapping("/page")
    public Result<IPage<JobDataMapVo>> list(IPage<JobDataMapVo> page, ExecutionJobQueryDto queryDto) {
        executionJobService.fetchPageJobDataMap(page, queryDto);
        return Result.ok(page);
    }


    /**
     * 获取定时任务详细信息
     */
    @GetMapping(value = "/{jobId}")
    public Result<JobDataMapVo> getInfo(@PathVariable("jobId") String jobId) {
        return Result.ok(executionJobService.fetchJobDataMap(jobId));
    }


    /**
     * 修改定时任务
     */
    @PutMapping("/edit")
    public Result<String> edit(@RequestBody ExecutionJobDto executionJob) throws SchedulerException {
        if (!CronUtils.isValid(executionJob.getCronExpression())) {
            return Result.fail("修改任务'" + executionJob.getUuid() + "'失败，Cron表达式不正确");
        }
        executionJobService.updateCronJob(executionJob);
        return Result.ok();
    }

    /**
     * 定时任务状态修改
     */
    @PutMapping("/changeStatus/{uuid}")
    public Result<String> changeStatus(@PathVariable String uuid, Integer status) throws SchedulerException {
        if (ScheduleConstants.ExecutionJobStatus.NORMAL.getValue().equals(status)) {
            executionJobService.resumeJob(uuid);
        } else if (ScheduleConstants.ExecutionJobStatus.PAUSE.getValue().equals(status)) {
            executionJobService.pauseJob(uuid);
        }
        return Result.ok();
    }

    /**
     * 定时任务立即执行一次
     */
    @PutMapping("/execute/{uuid}")
    public Result<String> execute(@PathVariable String uuid) throws SchedulerException {
        executionJobService.execute(uuid);
        return Result.ok();
    }

    /**
     * 删除定时任务
     */
    @DeleteMapping("/{jobIds}")
    public Result<String> remove(@PathVariable String[] jobIds) throws SchedulerException {
        for (String jobId : jobIds) {
            executionJobService.deleteJob(jobId);
        }
        return Result.ok();
    }
}
